package com.lanou.zy.projecta.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.entity.Srn;
import com.lanou.zy.projecta.util.DataChange;
import com.lanou.zy.projecta.util.OnItemClickListener;
import com.lanou.zy.projecta.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/11/6.
 */
public class SrnRecyclerAdapter extends RecyclerView.Adapter<SrnRecyclerAdapter.SrnViewHolder> {
    private List<Srn.DataEntity.EntryEntity> entities;
    private ImageLoader imageLoader;

    public SrnRecyclerAdapter(Context c) {
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    public void addData(List<Srn.DataEntity.EntryEntity> datas) {
        this.entities = datas;
        notifyDataSetChanged();
    }

    @Override
    public SrnViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View v = inflater.inflate(R.layout.searchresult_notes_recyclerview_item, null);
        return new SrnViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final SrnViewHolder holder, int position) {
        if (entities != null && entities.size() > 0) {
            Srn.DataEntity.EntryEntity sde = entities.get(position);
            holder.srn_Commit_Tv.setText(sde.getReplys());
            holder.srn_Content_Tv.setText(sde.getTitle());
            holder.srn_Time_Tv.setText(DataChange.getStrTime(sde.getLastpost() + ""));
            holder.srn_UsersName_Tv.setText(sde.getUsername());
            holder.srn_Views_Tv.setText(sde.getViews());
        }

        if (itemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getAdapterPosition();
                    itemClickListener.onItemClick(holder.itemView, position);
                }
            });
        }

    }

    private OnItemClickListener itemClickListener;

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public int getItemCount() {
        return entities != null && entities.size() > 0 ? entities.size() : 0;
    }

    public class SrnViewHolder extends RecyclerView.ViewHolder {
        private TextView srn_Content_Tv, srn_UsersName_Tv, srn_Time_Tv, srn_Views_Tv, srn_Commit_Tv;

        public SrnViewHolder(View v) {
            super(v);
            srn_Time_Tv = (TextView) v.findViewById(R.id.srn_Time_Tv);
            srn_Views_Tv = (TextView) v.findViewById(R.id.srn_Views_Tv);
            srn_Commit_Tv = (TextView) v.findViewById(R.id.srn_Commit_Tv);
            srn_Content_Tv = (TextView) v.findViewById(R.id.srn_Content_Tv);
            srn_UsersName_Tv = (TextView) v.findViewById(R.id.srn_UsersName_Tv);
        }
    }
}
